package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.LiveRoomInfo;
import com.ymatou.iapi.livebuyer.parameter.QueryCommendProductsBean;
import com.ymatou.iapi.livebuyer.parameter.RecommendProductDto;
import com.ymatou.iapi.livebuyer.service.QueryCommendProductsCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*获取直播间的推荐商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryCommendProducts{
	private static QueryCommendProductsBean querycommendproductsBean;
	private static QueryCommendProductsCall querycommendproductsCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取直播间的推荐商品");
	}

	@Before
	public void caseUp() {
		querycommendproductsBean=new QueryCommendProductsBean();
		querycommendproductsCall=new QueryCommendProductsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querycommendproductsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryCommendProducts_001() {
		Logger.start(true,"获取直播间的推荐商品-秒杀");
		try {
			//第几个商品参加秒杀
			int scekillno=2;
			//第几个商品置顶
			int topno=4;
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			
			
			List<String> ProductIdList=new ArrayList<>();
			for (int i = 0; i < 5; i++) {
				Logger.comment("创建商品");
				AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
				String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
				ProductIdList.add(productId);
			}
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, ProductIdList);
			
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(ProductIdList.get(scekillno)).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			starttime = new Date(YMTDateUtil.getDate().getTime() + 1 * 1000);
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), ProductIdList.get(topno));
			
			Thread.sleep(2000);
			List<Integer> list = Arrays.asList(createlive.getId());
			querycommendproductsBean.setLiveIds(list);
			querycommendproductsCall.setData(querycommendproductsBean);
			querycommendproductsCall.callService();			
			Logger.verifyEquals("200", querycommendproductsCall.getString("code"), "判断code");
			ArrayList<RecommendProductDto> liveroominfolist = querycommendproductsCall.getRecommendProductDtos();
			Logger.verifyEquals(1, liveroominfolist.size(), "验证露出数量");
			Logger.verifyEquals(ProductIdList.get(scekillno),liveroominfolist.get(0).getProductId(), "验证RecommendProduct1-scekill");
			Logger.verifyEquals(true,liveroominfolist.get(0).getInSecKill(), "验证RecommendProduct1-getInSecKill");
//			Logger.verifyEquals(ProductIdList.get(topno),liveroominfolist.get(0).getRecommendProducts().get(1).getProductId(), "验证RecommendProduct2-top");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryCommendProducts_002() {
		Logger.start(true,"获取直播间的推荐商品-置顶");
		try {
			//第几个商品参加秒杀
			int scekillno=2;
			//第几个商品置顶
			int topno=4;
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			
			
			List<String> ProductIdList=new ArrayList<>();
			for (int i = 0; i < 5; i++) {
				Logger.comment("创建商品");
				AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
				String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
				ProductIdList.add(productId);
			}
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, ProductIdList);
			
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(ProductIdList.get(scekillno)).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
//			Logger.comment("创建秒杀活动");
//			starttime = new Date(YMTDateUtil.getDate().getTime() + 2 * 1000);
//			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
//			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), ProductIdList.get(topno));
			
			Thread.sleep(2000);
			List<Integer> list = Arrays.asList(createlive.getId());
			querycommendproductsBean.setLiveIds(list);
			querycommendproductsCall.setData(querycommendproductsBean);
			querycommendproductsCall.callService();			
			Logger.verifyEquals("200", querycommendproductsCall.getString("code"), "判断code");
			ArrayList<RecommendProductDto> liveroominfolist = querycommendproductsCall.getRecommendProductDtos();
			Logger.verifyEquals(1, liveroominfolist.size(), "验证露出数量");
//			Logger.verifyEquals(ProductIdList.get(scekillno),liveroominfolist.get(0).getProductId(), "验证RecommendProduct1-scekill");
			Logger.verifyEquals(false,liveroominfolist.get(0).getInSecKill(), "验证RecommendProduct1-getInSecKill");
			Logger.verifyEquals(ProductIdList.get(topno),liveroominfolist.get(0).getProductId(), "验证RecommendProduct2-top");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	//liveRoomWapper.updateLiveactivityByProduct(ProductIdList.get(scekillno), 0, 0);
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryCommendProducts_003() {
		Logger.start(true,"获取直播间的推荐商品-售罄不展示");
		try {
			//第几个商品参加秒杀
			int scekillno=2;
			//第几个商品置顶
			int topno=4;
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			
			
			List<String> ProductIdList=new ArrayList<>();
			for (int i = 0; i < 5; i++) {
				Logger.comment("创建商品");
				AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
				String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
				ProductIdList.add(productId);
			}
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, ProductIdList);
			
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(ProductIdList.get(scekillno)).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			starttime = new Date(YMTDateUtil.getDate().getTime() + 2 * 1000);
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), ProductIdList.get(topno));
			new LiveRoomWapper().updateLiveactivityByProduct(ProductIdList.get(scekillno), 0, 0);
			Thread.sleep(2000);
			List<Integer> list = Arrays.asList(createlive.getId());
			querycommendproductsBean.setLiveIds(list);
			querycommendproductsCall.setData(querycommendproductsBean);
			querycommendproductsCall.callService();			
			Logger.verifyEquals("200", querycommendproductsCall.getString("code"), "判断code");
			ArrayList<RecommendProductDto> liveroominfolist = querycommendproductsCall.getRecommendProductDtos();
			Logger.verifyEquals(1, liveroominfolist.size(), "验证露出数量");
//			Logger.verifyEquals(ProductIdList.get(scekillno),liveroominfolist.get(0).getProductId(), "验证RecommendProduct1-scekill");
			//售罄的秒杀不会展示 所以露出top
			Logger.verifyEquals(false,liveroominfolist.get(0).getInSecKill(), "验证RecommendProduct1-getInSecKill");
			Logger.verifyEquals(ProductIdList.get(topno),liveroominfolist.get(0).getProductId(), "验证RecommendProduct2-top");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}